<?
include('../scaffold/tag.php');
include('../controller/handler.php');
include('../controller/philomousia.php');

include('../module/var.inc.php');

session_start();

$hd = new Handler();
$hd->openDB();
$base_url = $hd->getBaseURL();
if($hd->getPassword()==sha1($_SESSION['password'])) {
	$ph = new Philomousia();
	if(isset($_GET['id'])) {
		$ph->deleteTag($_GET['id'], $hd);
		$feedback = "Tag eliminato con successo, ti ricordo che anche tutti i riferimenti degli articoli ".
			"al tag eliminato sono stati cancellati.";
	} else if($_POST!=NULL) {
		if($_POST['sent_new']) {
			$ph->insertTag($_POST['new_tag'], $hd);
			$feedback = "Tag aggiunto con successo.";
		} else {
			$ph->manageTags($_POST, $hd);
			$feedback = "Tag modificati con successo, ti ricordo che tutti gli articoli precedentemente ".
				"inseriti, da questo momento faranno riferimento ai nuovi tag.";
		}
	}
}

$tags = $ph->loadTags($hd);
$hd->closeDB();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
	<? include('../module/head.inc.php'); ?>
	<meta name="robots" content="noindex, nofollow"/>
</head>

<body>
<div id="content">
<?include('../module/header.inc.php');?>

<?if($hd->getPassword()==sha1($_SESSION['password'])) {?>

<div class="form">
	<div class="upper_bar_main"><div class="upper_bar_round"></div></div>
	<div class="body_main_left">
	<div class="body_main_right">
	<div class="body_main">
	<form action="<?echo $base_url;?>panel/tag" method="post" accept-charset="utf-8" onsubmit="return newTagForm(this);;">
		<h2>Aggiungi nuovo argomento</h2>
		<input type="text" class="text" name="new_tag" value="" /> Nome argomento
		<input type="hidden" name="sent_new" value="true" />
		<p><input type="submit" value="Aggiungi &rarr;"></p>
	</form>
	<?if($feedback) {?><p id="feedback" class="highline"><?echo $feedback;?></p> <?}?>
	<p class="small admin_action">
		<a href="<?echo $base_url;?>panel">&larr; Pannello di amministrazione</a> |
		<a href="<?echo $base_url;?>panel/article">Aggiungi articolo</a>
	</p>
	</div></div></div>
	<div class="lower_bar_main"><div class="lower_bar_round"></div></div>
</div>

<?if(count($tags)>0) {?>
<div class="form">
<div class="upper_bar_main"><div class="upper_bar_round"></div></div>
<div class="body_main_left">
<div class="body_main_right">
<div class="body_main">
<form action="<?echo $base_url;?>panel/tag" method="post" accept-charset="utf-8" id="tag_form" onsubmit="return tagForm(this);;">
	<h2>Modifica argomenti</h2>
	<?foreach($tags as $tag) {?>
	<input type="text" class="text" name="<?echo $tag->getId();?>" value="<?echo $tag->getName();?>" /> 
	<a href="<?echo $base_url;?>panel/tag/delete:<?echo $tag->getId();?>">Elimina</a><br />
	<?}?>
	<p><input type="submit" value="Modifica &rarr;" /></p>
</form>
</div></div></div>
<div class="lower_bar_main"><div class="lower_bar_round"></div></div>
</div>
<?}?>

<?}?>

<? include('../module/footer.inc.php'); ?>
</div>
</body>
</html>
